# -*- coding: utf-8 -*-

"""
@Datetime: 2019/3/31
@Author: Zhang Yafei
sklearn中的距离矩阵
PAIRED_DISTANCES = {
    'cosine': paired_cosine_distances,
    'euclidean': paired_euclidean_distances,
    'l2': paired_euclidean_distances,
    'l1': paired_manhattan_distances,
    'manhattan': paired_manhattan_distances,
    'cityblock': paired_manhattan_distances}
"""
import numpy as np
import pandas as pd
from scipy.spatial import distance
from sklearn.metrics import euclidean_distances
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.metrics.pairwise import paired_distances
from sklearn.preprocessing import StandardScaler

if __name__ == '__main__':
    # 1. 读取数据
    beer = pd.read_csv('data.txt', sep=' ')
    # 2. 读取特征X, 并标准化
    X = beer[beer.columns[beer.columns != 'name']].values
    X = StandardScaler().fit_transform(X)
    # 3. 相似度
    # 余弦相似度矩阵
    cosine_simi_matrix = cosine_similarity(X)
    # 欧氏距离矩阵
    eu_simi_matrix = euclidean_distances(X=X, Y=X)
    # 曼哈顿距离
    vector1 = np.mat([1, 2, 3])
    vector2 = np.mat([4, 5, 6])
    manhattan_simi = paired_distances(X=vector1, Y=vector2, metric='manhattan')
    print(manhattan_simi)
    # jacard相似系数
    X = np.matrix([[1, 1, 0, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 0, 1, 1, 1]])
    jaccard = distance.pdist(X, metric='jaccard')[0]
    print(jaccard)
